package net.akcegaraz.tipleague.client.presenter;

import net.akcegaraz.tipleague.client.service.TipServiceAsync;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class HelloPresenter implements Presenter {
    public interface Display {
	Widget asWidget();

	void hello(String result);
    }

    private final TipServiceAsync rpcService;
    private final HandlerManager eventBus;

    private final Display display;

    public HelloPresenter(TipServiceAsync rpcService, HandlerManager eventBus,
	    Display display) {
	this.rpcService = rpcService;
	this.eventBus = eventBus;
	this.display = display;
    };

    public void go(HasWidgets container) {
	container.clear();
	container.add(display.asWidget());
	fetchHello();
    }

    private void fetchHello() {
	rpcService.hello(new AsyncCallback<String>() {

	    public void onFailure(Throwable arg0) {
		Window.alert("Error fetch hello");

	    }

	    public void onSuccess(String result) {
		// eventBus.fireEvent(new HelloEvent(result));
		display.hello(result);
	    }
	});
    }
}
